/* 
 * 微信分享JS调用
 * @author 赖来基
 * @requires jQuery v1.4.3+
 */
(function ($) {
    $.wxshare = function (_data) {
        var _default = {'title': document.title, 'desc': '室友网是全球合租房源频道的先行者，坚持以免费、易用，可靠的原则，致力于服务世界各地学生及社会人士，为广大室友们倾力打造寻房租屋、招募室友、分享生活', 'link': window.location.href, 'imgUrl': 'http://www.roomie.hk/assets/img/logo300.jpg'};
        var _data = $.extend(_default, _data);
        var wxsrc = "http://res.wx.qq.com/open/js/jweixin-1.0.0.js";
        /**
         * 微信统计
         * @param {type} _data
         */
        var wxtongji = function (action_type) {
            var _url=base_url_www + 'wechat/tongji?url='+encodeURIComponent(_data.link)+'&title='+encodeURIComponent(_data.title)+'&actiontype='+action_type;
            $.ajax({
                url: _url,
                dataType: "jsonp",
                jsonp: "cb",
                success: function () {
                    return true;
                }
            });
        }

        $.getScript(wxsrc).done(function () {//异步加载完成
            var _url=base_url_www + 'wechat/getWechatSign';
            $.ajax({
                url: _url,
                dataType: "jsonp",
                jsonp: "cb",
                success: function (data) {
                    //微信配置
                    wx.config({
                        debug: false,
                        appId: data.appId,
                        timestamp: data.timestamp,
                        nonceStr: data.nonceStr,
                        signature: data.signature,
                        jsApiList: ['checkJsApi',
                            'onMenuShareTimeline',
                            'onMenuShareAppMessage',
                            'onMenuShareQQ',
                            'onMenuShareWeibo']
                    });

                    wx.ready(function () {
                        bindSelector(wx);
                    });
                    wx.error(function (res) {
                        alert(res.errMsg);
                    });
                }
            });
            /**
             * 事件绑定
             */
            var bindSelector = function (wx) {
                // 2.1 监听“分享给朋友”，按钮点击、自定义分享内容及分享结果接口
                wx.onMenuShareAppMessage({
                    title: _data.title,
                    desc: _data.desc,
                    link: _data.link,
                    imgUrl: _data.imgUrl,
                    trigger: function (res) {
                    },
                    success: function (res) {
                        wxtongji('friends');
                        $('#mcover').hide();
                    },
                    cancel: function (res) {
                    },
                    fail: function (res) {
                    }
                });

                // 2.2 监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口
                wx.onMenuShareTimeline({
                    title: _data.title,
                    desc: _data.desc,
                    link: _data.link,
                    imgUrl: _data.imgUrl,
                    trigger: function (res) {
                    },
                    success: function (res) {
                        wxtongji('moments');
                        $('#mcover').hide();
                    },
                    cancel: function (res) {
                    },
                    fail: function (res) {

                    }
                });
                // 2.3 监听“分享到QQ”按钮点击、自定义分享内容及分享结果接口
                wx.onMenuShareQQ({
                    title: _data.title,
                    desc: _data.desc,
                    link: _data.link,
                    imgUrl: _data.imgUrl,
                    trigger: function (res) {
                    },
                    complete: function (res) {

                    },
                    success: function (res) {
                        $('#mcover').hide();
                    },
                    cancel: function (res) {
                    },
                    fail: function (res) {
                    }
                });

                // 2.4 监听“分享到微博”按钮点击、自定义分享内容及分享结果接口
                wx.onMenuShareWeibo({
                    title: _data.title,
                    desc: _data.desc,
                    link: _data.link,
                    imgUrl: _data.imgUrl,
                    trigger: function (res) {
                    },
                    complete: function (res) {

                    },
                    success: function (res) {
                        $('#mcover').hide();
                    },
                    cancel: function (res) {

                    },
                    fail: function (res) {

                    }
                });
            }
        });
        return this;
    };
})(jQuery);